package com.swcn.u_pet;

import java.util.Vector;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class AccelerometerListener implements SensorEventListener {

	float[] inR = new float[16];
	float[] outR = new float[16];
	float[] I = new float[16];

	int size;
	float before;
	float after;
	float[] size_of_change = new float[3];

	private float orientation[] = new float[3];

	private float magneticField[] = new float[3];

	private float accel[] = new float[3];

	private int magneticFieldSensorAccuracy, accelerometerAccuracy;

	static Vector<Float> x = new Vector<Float>();
	static Vector<Float> y = new Vector<Float>();
	static Vector<Float> z = new Vector<Float>();

	private MainView mainView;
	Steps steps;
	private boolean mstepFlag = false;

	public AccelerometerListener(MainView view) {
		for (int i = 0; i < 3; i++) {
			orientation[i] = 0.0f;
			accel[i] = 0.0f;
			magneticField[i] = 0.0f;
		}
		mainView = view;
	}

	public AccelerometerListener(Steps view) {
		for (int i = 0; i < 3; i++) {
			orientation[i] = 0.0f;
			accel[i] = 0.0f;
			magneticField[i] = 0.0f;
		}
		steps = view;
	}

	public float getAzimuth() {
		return (int) Math.toDegrees(orientation[0]);

	}

	public float getPitch() {
		return (int) Math.toDegrees(orientation[1]);
	}

	public float getRoll() {
		return (int) Math.toDegrees(orientation[2]);
	}

	public float getAccelX() {
		return accel[0];
	}

	public float getAccelY() {
		return accel[1];
	}

	public float getAccelZ() {
		return accel[2];
	}

	public int getMagneticFieldSensorAccuracyr() {
		return magneticFieldSensorAccuracy;
	}

	public int getAccelerometeAccuracyr() {
		return accelerometerAccuracy;
	}

	public void onAccuracyChanged(Sensor arg0, int arg1) {
		if (arg0.getType() == Sensor.TYPE_MAGNETIC_FIELD)
			magneticFieldSensorAccuracy = arg1;
		else if (arg0.getType() == Sensor.TYPE_ACCELEROMETER)
			accelerometerAccuracy = arg1;
	}

	public void onSensorChanged(SensorEvent event) {

		if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
			for (int i = 0; i < 3; i++)
				magneticField[i] = event.values[i];
		} else if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
			for (int i = 0; i < 3; i++) {
				accel[i] = event.values[i];
				if (i == 0) {
					x.add(event.values[i]);
				}
				if (i == 1) {
					y.add(event.values[i]);
				}
				if (i == 2) {
					z.add(event.values[i]);
				}
			}
		}

		SensorManager.getRotationMatrix(inR, I, accel, magneticField);
		SensorManager.remapCoordinateSystem(inR, SensorManager.AXIS_X,
				SensorManager.AXIS_Z, outR);
		SensorManager.getOrientation(outR, orientation);

		mainView.invalidate();
		// steps.invalidate();
		size = x.size();
		if (size > 2) {
			before = (Float) x.get(size - 2);
			after = (Float) x.get(size - 1);
			size_of_change[0] = Math.abs(before - after);
		}
		size = y.size();
		if (size > 2) {
			before = (Float) y.get(size - 2);
			after = (Float) y.get(size - 1);
			size_of_change[1] = Math.abs(before - after);
		}
		size = z.size();
		if (size > 2) {
			before = (Float) z.get(size - 2);
			after = (Float) z.get(size - 1);
			size_of_change[2] = Math.abs(before - after);
		}
		if (mstepFlag == true) {

			if ((size_of_change[1] + size_of_change[2] + size_of_change[0]) < 9.6138) {
				mainView.Count();
				mstepFlag = false;
			}
		}
		else{
			if((size_of_change[1] + size_of_change[2] + size_of_change[0]) > 11.0){
				mstepFlag = true;
			}
		}
		size = x.size();
		float temp = 0;
		if (size > 10000) {
			temp = (Float) x.get(size - 1);
			x.removeAllElements();
			x.add(temp);
		}

		size = y.size();
		if (size > 10000) {
			temp = (Float) y.get(size - 1);
			y.removeAllElements();
			y.add(temp);
		}
		size = z.size();
		if (size > 10000) {
			temp = (Float) z.get(size - 1);
			z.removeAllElements();
			z.add(temp);
		}

	}
}
